<?php
/*
SMF Links
Version 1.4
by:vbgamer45
http://www.smfhacks.com
*/
function template_main()
{
	global $db_prefix, $scripturl, $txt,$settings,$modSettings;

	//To get Permissions text
	loadLanguage('Admin');

	echo '<div class="tborder">';
	//See if they can approve links
	$a_links = allowedTo('approve_links');
	$addlink = allowedTo('add_links');

	//Get the Category
	$cat = (int) @$_REQUEST['cat'];
	//Check if there was a category
	if(empty($cat))
	{
		//No category found show the links index
		echo '<h1 align="center"><b>' . $txt['smflinks_indextitle'] . '</b></h1>';

		//Save the result of managing categorys
		$m_cats = allowedTo('links_manage_cat');

		$ratelink = allowedTo('rate_links');

		//List all the catagories
		$dbresult = db_query("SELECT ID_CAT, title, image, roworder, description FROM {$db_prefix}links_cat ORDER BY roworder ASC", __FILE__, __LINE__);
		//Get category count
		$cat_count = db_affected_rows();
			
		echo '<table cellspacing="0" cellpadding="10" border="0" align="center" width="90%" class="tborder">
			<tr>
				<td class="titlebg" colspan="2">' . $txt['smflinks_ctitle'] . '</td>
				<td class="titlebg">' . $txt['smflinks_description'] .'</td>
				<td class="titlebg">' . $txt['smflinks_totallinks'] . '</td>';
				if($m_cats)
					echo '<td class="titlebg">' . $txt['smflinks_options'] .'</td>';
		echo '
			</tr>';
			
		while($row = mysql_fetch_assoc($dbresult))
		{
			
		  	$dbresult2 = db_query("SELECT title FROM {$db_prefix}links WHERE ID_CAT = ". $row['ID_CAT'] . ' AND approved = 1', __FILE__, __LINE__);
			$totallinks = db_affected_rows();
			
			echo '<tr>';
			
			if(function_exists('parse_bbc'))
			{
				
				if($row['image'] == '')
					echo '<td colspan="2" class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '">' . parse_bbc($row['title']) . '</a></td><td class="windowbg2">' . parse_bbc($row['description']) . '</td>';
				else
				{
					echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '"><img src="' . $row['image'] . '" border="0" /></a></td>';
					echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '">' . parse_bbc($row['title']) . '</a></td><td class="windowbg2">' . parse_bbc($row['description']) . '</td>';
				}
				
			}
			else
			{
				
				if($row['image'] == '')
					echo '<td colspan="2" class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '">' . doUBBC($row['title']) . '</a></td><td class="windowbg2">' . doUBBC($row['description']) . '</td>';
				else
				{
					echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '"><img src="' . $row['image'] . '" border="0" /></a></td>';
					echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '">' . doUBBC($row['title']) . '</a></td><td class="windowbg2">' . doUBBC($row['description']) . '</td>';
				}
				

			}
			
			echo '<td class="windowbg2">' . $totallinks . '</td>';
			

			//Show Edit Delete and Order category
			if($m_cats)
			{
				echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;sa=catup;cat=' . $row['ID_CAT'] . '">' . $txt['smflinks_txtup'] . '</a>&nbsp;<a href="' . $scripturl . '?action=links;sa=catdown;cat=' . $row['ID_CAT'] . '">' . $txt['smflinks_txtdown'] . '</a></span>&nbsp;<a href="' . $scripturl . '?action=links;sa=editcat;cat=' . $row['ID_CAT'] . '">' . $txt['smflinks_txtedit'] . '</a>&nbsp;<a href="' . $scripturl . '?action=links;sa=deletecat;cat=' . $row['ID_CAT'] . '">' . $txt['smflinks_txtdel'] . '</a></td>';
			}
			
			mysql_free_result($dbresult2);
			
			echo '</tr>';
		}
		
		echo '</table>';
		mysql_free_result($dbresult);

		//Permissions for Top 5 Rated, Top 5 Hits and Approval
		$editlink = allowedTo('edit_links');
		$deletelink = allowedTo('delete_links');

echo '<table border="0" cellpadding="0" cellspacing="0" width="90%" align="center">
  <tr>
    <td width="50%" valign="top">';
			
		//Show Top 5 rated
		if(!empty($modSettings['smflinks_setshowtoprate']))
		{
			echo '<div class="tborder" style="margin: 2%;"><div class="catbg2" align="center">' . $txt['smflinks_topfiverated'] . '</div>';
			echo '<table align="center">';

			$dbresult = db_query("SELECT l.ID_LINK, l.rating, l.title,l.date, m.realName, l.ID_MEMBER, l.description,l.hits FROM {$db_prefix}links AS l LEFT JOIN {$db_prefix}members AS m ON (l.ID_MEMBER = m.ID_MEMBER)  WHERE l.approved = 1 ORDER BY l.rating DESC LIMIT 5", __FILE__, __LINE__);
			while($row = mysql_fetch_assoc($dbresult))
			{
				echo '<tr>
				<td align="center">
				<a href="' . $scripturl . '?action=links;sa=visit;id=' . $row['ID_LINK'] . '" target="blank">' . $row['title'] . '</a>&nbsp;';
			
		
				echo $txt['smflinks_rating'] . $row['rating'];
				
				if($editlink)
					echo '&nbsp;<a href="' . $scripturl . '?action=links;sa=editlink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtedit'] . '</a>&nbsp;';
				if($deletelink)
					echo '<a href="' . $scripturl . '?action=links;sa=deletelink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtdel'] . '</a>';
	
				echo '</td></tr>';
			}
			echo '</table>';
			mysql_free_result($dbresult);
			echo '</div>';
		}

	echo '</td>';
		//Show Top 5 hits
	echo '<td width="50%" valign="top">';
		if(!empty($modSettings['smflinks_setshowmostvisited']))
		{
			echo '<div class="tborder" style="margin: 2%;"><div class="catbg2" align="center">' . $txt['smflinks_topfivevisited'] . '</div>';
			echo '<table align="center">';
			$dbresult = db_query("SELECT l.ID_LINK, l.rating, l.title,l.date, m.realName, l.ID_MEMBER, l.description,l.hits FROM {$db_prefix}links AS l LEFT JOIN {$db_prefix}members AS m  ON (l.ID_MEMBER = m.ID_MEMBER) WHERE l.approved = 1 ORDER BY l.hits DESC LIMIT 5", __FILE__, __LINE__);
			while($row = mysql_fetch_assoc($dbresult))
			{
				
				echo '<tr>
						<td align="center">';
				echo '<a href="' . $scripturl . '?action=links;sa=visit;id=' . $row['ID_LINK'] . '" target="blank">' . $row['title'] . '</a>&nbsp;';

				echo $txt['smflinks_hits'] . $row['hits'] . '&nbsp;';
	
				
				if($editlink)
					echo '<a href="' . $scripturl . '?action=links;sa=editlink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtedit'] . '</a>&nbsp;';
				if($deletelink)
					echo '<a href="' . $scripturl . '?action=links;sa=deletelink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtdel'] . '</a>';
	
				echo '</td></tr>';
				
				

			}
			echo '</table>';
			mysql_free_result($dbresult);
			echo '</div>';
		}
		
	echo '</td>';
	echo '</tr>
	</table>';
		//####################

		//See if they are allowed to add catagories Main Index only
		if($m_cats)
		{
			echo '<div class="tborder" style="margin: 2%;"><div class="catbg2" align="center">' . $txt['smflinks_linkspanel'] . '</div>';
			echo '<div align="center" class="windowbg2"><a href="' . $scripturl . '?action=links;sa=addcat">' . $txt['smflinks_addcat'] . '</a>&nbsp;';
			if($addlink)
				echo '<a href="' . $scripturl . '?action=links;sa=addlink">' . $txt['smflinks_addlink'] . '</a>&nbsp;';
			
			echo '<a href="' . $scripturl . '?action=links;sa=admin">' . $txt['smflinks_linkssettings'] . '</a>&nbsp;';
			
				
			if (allowedTo('manage_permissions'))
				echo '<a href="' . $scripturl . '?action=permissions">' . $txt['edit_permissions'] . '</a>';

			echo '</div></div>';
		}
		//See if they can approve links
		if($a_links)
		{
			$dbresult = db_query("SELECT l.ID_LINK FROM {$db_prefix}links AS l WHERE l.approved = 0", __FILE__, __LINE__);
			$alinks_total = db_affected_rows();
			echo '<div class="tborder" style="margin: 2%;"><div class="catbg2" align="center">' . $txt['smflinks_approvepanel'] . '</div>';
			echo '<div align="center" class="windowbg2"><a href="' . $scripturl . '?action=links;sa=alist">' . $txt['smflinks_approvelinks'] . '</a><br />';
			echo $txt['smflinks_thereare'] . '<b>' . $alinks_total . '</b>' . $txt['smflinks_waitingapproval'];
			echo '</div></div>';
			mysql_free_result($dbresult);
		}

		//Stats
		if(!empty($modSettings['smflinks_setshowstats']))
		{
			$dbresult = db_query("SELECT ID_LINK FROM {$db_prefix}links WHERE approved = 1", __FILE__, __LINE__);
			$link_count = db_affected_rows();
			echo '<div class="tborder" style="margin: 2%;"><div class="catbg2" align="center">' . $txt['smflinks_stats'] . '</div>';
			echo '<div align="center" class="windowbg2">' . $txt['smflinks_thereare'] . '<b>' . $cat_count . '</b>' . $txt['smflinks_catand'] . '<b>' . $link_count . '</b>' . $txt['smflinks_linkssystem'] . '</div></div>';
			mysql_free_result($dbresult);
		}
	}
	else
	{
		//Category found show the links


		//Get the category name
		$dbresult = db_query("SELECT ID_CAT, title, description FROM {$db_prefix}links_cat WHERE ID_CAT = $cat LIMIT 1", __FILE__, __LINE__);
		$row = mysql_fetch_assoc($dbresult);
		mysql_free_result($dbresult);

		//Setup their permissions
		$editlink = allowedTo('edit_links');
		$deletelink = allowedTo('delete_links');
		$ratelink = allowedTo('rate_links');

		//Show top links Links - Category
		echo '<a href="' . $scripturl . '?action=links">' . $txt['smflinks_indextitle'] . '</a>>>' . $row['title'] . '<br />';

		echo '<h1 align="center"><b>' . $row['title'] . '</b></h1>';
		
		
		//Get the page number
		if(isset($_REQUEST['pg']))
			$page = (int) @$_REQUEST['pg'];
		else 
			$page = 1;
		
		if(empty($modSettings['smflinks_setlinksperpage']))
			$modSettings['smflinks_setlinksperpage'] = 10;
		
		//Get Total Pages
		$dbresult = db_query("SELECT COUNT(*) AS total FROM {$db_prefix}links WHERE ID_CAT = $cat  AND approved = 1", __FILE__, __LINE__);
		$row = mysql_fetch_assoc($dbresult);
		$total = ceil($row['total'] / $modSettings['smflinks_setlinksperpage']);
		mysql_free_result($dbresult);


		$page--;
		if($page < 0)
			$page = 0;

		$curpage = $page * $modSettings['smflinks_setlinksperpage'];

		//Show the links in that category
		$dbresult = db_query("SELECT l.ID_LINK,l.title,l.date, l.rating, m.realName, l.ID_MEMBER, l.description,l.hits FROM {$db_prefix}links AS l LEFT JOIN {$db_prefix}members AS m  ON (l.ID_MEMBER = m.ID_MEMBER) WHERE l.ID_CAT = $cat  AND l.approved = 1 ORDER BY l.ID_LINK DESC LIMIT $curpage," . $modSettings['smflinks_setlinksperpage'], __FILE__, __LINE__);

		//Check if no links where found
		if(db_affected_rows() == 0)
		{
			echo '<div class="tborder" style="margin: 2%;"><div class="catbg2" align="center">' . $txt['smflinks_nolinks2'] . $row['title'] . '</div>';
			echo '<div align="center" class="windowbg2">' . $txt['smflinks_nolinks'] . '</div></div>';

		}
		else
		{
			//There are links found in the category
			echo '<table cellspacing="0" cellpadding="10" border="0" align="center" width="90%" class="tborder">
					<tr>
						<td class="titlebg">' . $txt['smflinks_ctitle'] . '</td>
						<td class="titlebg">' . $txt['smflinks_description'] .'</td>
						<td class="titlebg">' . $txt['smflinks_chits'] . '</td>
						<td class="titlebg">' . $txt['smflinks_crating'] . '</td>
						<td class="titlebg">' . $txt['smflinks_cusername'] . '</td>
						<td class="titlebg">' . $txt['smflinks_cdate'] . '</td>
						<td class="titlebg">' . $txt['smflinks_options'] .'</td>
					</tr>';
			while($row = mysql_fetch_assoc($dbresult))
			{
				
				
				echo '<tr>';
				echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;sa=visit;id=' . $row['ID_LINK'] . '" target="blank">' . $row['title'] . '</a></td>';
				
				if(empty($modSettings['smflinks_setallowbbc']))
					echo '<td class="windowbg2">' . $row['description'] . '</td>';
				else 
					echo '<td class="windowbg2">' . doUBBC($row['description']) . '</td>';
				
				
				echo '<td class="windowbg2">' . $row['hits'] . '</td>';
				echo '<td class="windowbg2">' . $row['rating'];
				if($ratelink)
					echo '<br /><a href="' . $scripturl . '?action=links;sa=rate;value=1;id=' . $row['ID_LINK'] . '"><img src="', $settings['images_url'], '/post/thumbup.gif" alt="Good Link" border="0" /></a>&nbsp;&nbsp;<a href="' . $scripturl . '?action=links;sa=rate;value=0;id=' . $row['ID_LINK'] . '"><img src="', $settings['images_url'], '/post/thumbdown.gif" alt="Bad Link" border="0" /></a>';
	
				echo '</td>';
				//Check if it was a guest link
				if($row['realName'] != '')
					echo '<td class="windowbg2"><a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">'  . $row['realName'] . '</a></td>';
				else 
					echo '<td class="windowbg2">' . $txt['smflinks_txtguest'] . '</td>';
					
				echo '<td class="windowbg2">' . timeformat($row['date']) . '</td>';
				
				
				echo '<td class="windowbg2">';
				
				if($editlink)
					echo '<a href="' . $scripturl . '?action=links;sa=editlink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtedit'] . '</a>&nbsp;';
				if($deletelink)
					echo '<a href="' . $scripturl . '?action=links;sa=deletelink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtdel'] . '</a>&nbsp;';
				if($a_links)
						echo '<a href="' . $scripturl . '?action=links;sa=noapprove;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtunapprove'] . '</a>';
				echo '</td>';
				
				echo '</tr>';
			}
			
			//Show the pages
			if($total > 0)
			{
				echo '<tr class="titlebg">
						<td align="left" colspan="7">
						' . $txt['smflinks_pages'];
		
						for($i = 1; $i <= $total; $i++)
						{
							if($i == ($page+1))
								echo '&nbsp;[<a href="' . $scripturl . '?action=links;cat=' . $cat . ';pg=' . $i . '">' . $i . '</a>]';
							else
								echo '&nbsp;<a href="' . $scripturl . '?action=links;cat=' . $cat . ';pg=' . $i . '">' . $i . '</a>';
						}
		
				echo '
						</td>
					</tr></table>';
			}
			

			
			mysql_free_result($dbresult);
		}
			//Show return to links index link
			echo '
			<table cellspacing="0" cellpadding="10" border="0" align="center" width="90%" class="tborder">
			<tr class="titlebg"><td colspan="7" align="center">';
			
			
		
			//See if they are allowed to add links
			if($addlink)
			{
				echo '<a href="' . $scripturl . '?action=links;sa=addlink;cat=' . $cat . '">' . $txt['smflinks_addlink'] . '</a>&nbsp;';
				if (allowedTo('manage_permissions'))
					echo '<a href="' . $scripturl . '?action=permissions">' . $txt['edit_permissions'] . '</a>';
				echo '<br /><br />';
			}

			echo '
			<a href="' . $scripturl . '?action=links">'. $txt['smflinks_returnindex'] . '</a>';	
			echo '</td></tr></table>';



	}



	echo '</div>';
	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}

function template_addcat()
{
	global $scripturl, $txt;

	echo '<div class="tborder" >
<form method="POST" action="' . $scripturl . '?action=links&sa=addcat2">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" height="129">
  <tr>
    <td width="50%" colspan="2" height="19" align="center" class="catbg">
    <b>' . $txt['smflinks_addcat'] . '</b></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2" align="right"><span class="gen"><b>' . $txt['smflinks_ctitle'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="title" size="64" maxlength="100" /></td>
  </tr>
  <tr>
    <td width="28%" height="19" valign="top" class="windowbg2" align="right"><span class="gen"><b>' . $txt['smflinks_description'] . '</b></span></td>
    <td width="72%" height="19" class="windowbg2"><textarea rows="6" name="description" cols="54"></textarea></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2" align="right"><span class="gen"><b>' . $txt['smflinks_image'] . '</b>&nbsp;</span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="image" size="64" maxlength="100" /></td>
  </tr>
  <tr>
    <td width="28%" colspan="2" height="26" align="center" class="windowbg2">
    <input type="submit" value="' . $txt['smflinks_addcat'] . '" name="submit" /></td>

  </tr>
</table>
</form>
</div>';
	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}
function template_editcat()
{
	global $db_prefix, $scripturl, $txt;

	$cat = (int) @$_REQUEST['cat'];

	if(empty($cat))
		fatal_error($txt['smflinks_nocatselected']);

	$dbresult = db_query("SELECT ID_CAT, title, description, image, ID_PARENT FROM {$db_prefix}links_cat WHERE ID_CAT = $cat", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($dbresult);


echo '<div class="tborder" >
<form method="POST" action="' . $scripturl . '?action=links&sa=editcat2">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" height="129">
  <tr>
    <td width="50%" colspan="2" height="19" align="center" class="catbg">
    <b>' . $txt['smflinks_editcat'] . '</b></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2" align="right"><span class="gen"><b>' . $txt['smflinks_ctitle'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="title" size="64" maxlength="100" value="' . $row['title'] . '" /></td>
  </tr>
  <tr>
    <td width="28%" height="19" valign="top" class="windowbg2" align="right"><span class="gen"><b>' . $txt['smflinks_description'] . '</b></span></td>
    <td width="72%" height="19" class="windowbg2"><textarea rows="6" name="description" cols="54">' . $row['description'] . '</textarea></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2" align="right"><span class="gen"><b>' . $txt['smflinks_image'] . '</b>&nbsp;</span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="image" size="64" maxlength="100" value="' . $row['image'] . '" /></td>
  </tr>
  <tr>
    <td width="28%" colspan="2" height="26" align="center" class="windowbg2">
    <input type="hidden" value="' . $row['ID_CAT'] . '" name="catid" />
    <input type="submit" value="' . $txt['smflinks_editcat'] . '" name="submit" /></td>

  </tr>
</table>
</form>
</div>';

	mysql_free_result($dbresult);

	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}
function template_deletecat()
{
	global $db_prefix, $scripturl, $txt;

	$catid = (int) @$_REQUEST['cat'];

	if(empty($catid))
		fatal_error($txt['smflinks_nocatselected']);

	echo '<div class="tborder" >
	<form method="POST" action="' . $scripturl . '?action=links&sa=deletecat2">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" height="129">
  <tr>
    <td width="50%" colspan="2" height="19" align="center" class="catbg">
    <b>' . $txt['smflinks_deltcat'] . '</b></td>
  </tr>
  <tr>
    <td width="28%" colspan="2" height="26" align="center" class="windowbg2">
    <b>' . $txt['smflinks_warndel2'] . '</b>
    <br />
    <input type="hidden" value="' . $catid . '" name="catid" />
    <input type="submit" value="' . $txt['smflinks_deltcat'] . '" name="submit" /></td>
  </tr>
</table>
</form></div>';
	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}
function template_addlink()
{
	global $scripturl, $txt, $modSettings,$db_prefix;
	
	if(isset($_REQUEST['cat']))
		$catid = (int) @$_REQUEST['cat'];
	else 
		$catid = 0;
	
	$dbresult = db_query("SELECT ID_CAT, title, roworder FROM {$db_prefix}links_cat ORDER BY roworder ASC", __FILE__, __LINE__);
	
	//Get category count
	$cat_count = db_affected_rows();
	if($cat_count == 0)
		fatal_error($txt['smflinks_nofirstcat']);

	echo '<div class="tborder" >
<form method="POST" action="' . $scripturl . '?action=links&sa=addlink2">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" height="129">
  <tr>
    <td width="50%" colspan="2" height="19" align="center" class="catbg">
    <b>' . $txt['smflinks_addlink'] . '</b></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_ctitle'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="title" size="64" maxlength="100" /></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_category'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><select name="catid">';
		while($row = mysql_fetch_assoc($dbresult))
		{

  			echo '<option value="' . $row['ID_CAT'] . '" ' . (($row['ID_CAT'] == $catid) ? 'selected="selected" ' : '') .' ">' . $row['title'] . '</option>';

		}
echo '</select>
    </td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_url'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="url" size="64" maxlength="250" value="http://" /></td>
  </tr>
  <tr>
    <td width="28%" height="19" valign="top" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_description'] . '</b></span></td>
    <td width="72%" height="19" class="windowbg2"><textarea rows="6" name="description" cols="54"></textarea>';

	if(!empty($modSettings['smflinks_setallowbbc']))
	{
		echo '<br /><span class="smalltext">' . $txt['smflinks_bbcallowed'] . '</span>';
	}
    
   echo '
    </td>
  </tr>
  <tr>
    <td width="28%" colspan="2" height="26" align="center" class="windowbg2">
    <input type="submit" value="' . $txt['smflinks_addlink'] .'" name="submit" /></td>

  </tr>
</table>
</form></div>';
mysql_free_result($dbresult);
	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}
function template_editlink()
{
	global $db_prefix, $scripturl, $txt;
	//Get the link id
	$id = (int) @$_REQUEST['id'];

	if(empty($id))
		fatal_error($txt['smflinks_nolinkselected']);

	$dbresult = db_query("SELECT ID_LINK, title, ID_CAT, description, url FROM {$db_prefix}links WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($dbresult);
	
		


	echo '<div class="tborder" >
<form method="POST" action="' . $scripturl . '?action=links&sa=editlink2">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" height="129">
  <tr>
    <td width="50%" colspan="2" height="19" align="center" class="catbg">
    <b>' . $txt['smflinks_editlink'] . '</b></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_ctitle'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="title" size="64" maxlength="100" value="' . $row['title'] . '" /></td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_category'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><select name="catid">';
	$dbresult2 = db_query("SELECT ID_CAT, title, roworder FROM {$db_prefix}links_cat ORDER BY roworder ASC", __FILE__, __LINE__);
	
	//Get category count
	$cat_count = db_affected_rows();
	if($cat_count == 0)
		fatal_error($txt['smflinks_nofirstcat']);
		
		while($row2 = mysql_fetch_assoc($dbresult2))
		{

  			echo '<option value="' . $row2['ID_CAT'] . '" ' . (($row2['ID_CAT'] == $row['ID_CAT']) ? 'selected="selected" ' : '') .'>' . $row2['title'] . '</option>';

		}
		mysql_free_result($dbresult2);
echo '</select>
    </td>
  </tr>
  <tr>
    <td width="28%" height="22" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_url'] . '</b></span></td>
    <td width="72%" height="22" class="windowbg2"><input type="text" name="url" size="64" maxlength="250" value="' . $row['url'] . '" /></td>
  </tr>
  <tr>
    <td width="28%" height="19" valign="top" class="windowbg2"><span class="gen"><b>' . $txt['smflinks_description'] . '</b></span></td>
    <td width="72%" height="19" class="windowbg2"><textarea rows="6" name="description" cols="54">' . $row['description'] . '</textarea></td>
  </tr>
  <tr>
    <td width="28%" colspan="2" height="26" align="center" class="windowbg2">
    <input type="hidden" value="' . $id . '" name="id" />
    <input type="submit" value="' . $txt['smflinks_editlink'] . '" name="submit" /></td>

  </tr>
</table>
</form></div>';
	
	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}
function template_deletelink()
{
	global $scripturl, $txt;

	$id = (int) @$_REQUEST['id'];

	if(empty($id))
		fatal_error($txt['smflinks_nolinkselected']);

	echo '<div class="tborder" ><form method="POST" action="' . $scripturl . '?action=links&sa=deletelink2">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" height="129">
  <tr>
    <td width="50%" colspan="2" height="19" align="center" class="catbg">
    <b>' . $txt['smflinks_dellink'] . '</b></td>
  </tr>
  <tr>
    <td width="28%" colspan="2" height="26" align="center" class="windowbg2">
    <b>' . $txt['smflinks_warndel'] . '</b>
    <br />
    <input type="hidden" value="' . $id . '" name="id" />
    <input type="submit" value="' . $txt['smflinks_dellink'] . '" name="submit" /></td>
  </tr>
</table>
</form></div>';
	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}
function template_approvelinks()
{
	global $settings,$db_prefix,$scripturl,$txt;


	//Edit and Delete permissions
	$editlink = allowedTo('edit_links');
	$deletelink = allowedTo('delete_links');

	//Show all the links waiting for approval.
echo '<div class="tborder" ><h1 align="center">' . $txt['smflinks_approvelinks']  . '</h1>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" height="50">
  <tr>
    <td class="catbg" height="26"><b>' . $txt['smflinks_url'] . '</b></td>
    <td class="catbg" height="26"><b>' . $txt['smflinks_ctitle'] .'</b></td>
    <td class="catbg" height="26"><b>' . $txt['smflinks_description'] . '</b></td>
    <td class="catbg" height="26"><b>' .  $txt['smflinks_category'] . '</b></td>
    <td class="catbg" height="26"><b>' . $txt['smflinks_submittedby'] . '</b></td>
    <td class="catbg" height="26"><b>' . $txt['smflinks_options'] . '</b></td>
  </tr>';
 $dbresult = db_query("SELECT l.ID_LINK, l.approved,l.description, l.url, l.title,l.date, m.realName, l.ID_MEMBER, l.description,l.hits, l.ID_CAT, c.title catname FROM {$db_prefix}links AS l, {$db_prefix}links_cat AS c LEFT JOIN {$db_prefix}members AS m ON (l.ID_MEMBER = m.ID_MEMBER) WHERE l.ID_CAT = c.ID_CAT AND l.approved = 0 ORDER BY l.ID_LINK DESC", __FILE__, __LINE__);
while($row = mysql_fetch_assoc($dbresult))
{
  echo '<tr>';
  echo '<td class="windowbg2" height="26"><a href="' . $row['url'] . '" target="blank">' . $row['url'] . '</a></td>';
  echo '<td class="windowbg2" height="26">' . $row['title'] . '</td>';
  echo '<td class="windowbg2" height="26">' . $row['description'] . '</td>';
  echo '<td class="windowbg2" height="26">' . $row['catname'] . '</td>';

  if($row['realName'] == '')
  	 echo '<td class="windowbg2" height="26">' . $txt['smflinks_txtguest'] . '</td>';
  else
  	echo '<td class="windowbg2" height="26"><a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['realName'] . '</a></td>';
  
  echo '<td height="19" class="windowbg2"><a href="' . $scripturl . '?action=links;sa=approve;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtapprove'] . '</a>&nbsp;';
	if($editlink)
		echo '<a href="' . $scripturl . '?action=links;sa=editlink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtedit'] . '</a>&nbsp;';
	if($deletelink)
		echo '<a href="' . $scripturl . '?action=links;sa=deletelink;id=' . $row['ID_LINK'] . '">' . $txt['smflinks_txtdel'] .'</a>';

  echo '</td>';
  echo '</tr>';

 }
 mysql_free_result($dbresult);
 


echo '
</table>
<div align="center"><a href="' . $scripturl . '?action=links">' . $txt['smflinks_returnindex'] . '</a></div>
</div>
';




	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<br /><div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';
}

function template_manage_cats()
{
	global $scripturl,$db_prefix, $txt,$modSettings;
echo '

	<table border="0" width="80%" cellspacing="0" align="center" cellpadding="4" class="tborder">
		<tr class="titlebg">
			<td>' . $txt['smflinks_linkssettings'] . '</td>
		</tr>
		<tr class="windowbg">
			<td>
			<b>' . $txt['smflinks_managecats'] . '</b> - <span class="smalltext">' . $txt['smflinks_anagecats_des'] . '</span><br />';


		//List all the catagories
		$dbresult = db_query("SELECT ID_CAT, title, roworder, description FROM {$db_prefix}links_cat ORDER BY roworder ASC", __FILE__, __LINE__);
		//Get category count

		
		echo '<table cellspacing="0" cellpadding="10" border="0" align="center" width="90%" class="tborder">
			<tr>
				<td class="titlebg">' . $txt['smflinks_ctitle'] . '</td>
				<td class="titlebg">' . $txt['smflinks_description'] .'</td>
				<td class="titlebg">' . $txt['smflinks_totallinks'] . '</td>
				<td class="titlebg">' . $txt['smflinks_options'] .'</td>
			</tr>
		';
			
		while($row = mysql_fetch_assoc($dbresult))
		{
		  	$dbresult2 = db_query("SELECT ID_LINK FROM {$db_prefix}links WHERE ID_CAT = ". $row['ID_CAT'] . ' AND approved = 1', __FILE__, __LINE__);
			$totallinks = db_affected_rows();
			
			echo '<tr>';
			
			if(function_exists('parse_bbc'))
			{
				echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '">' . parse_bbc($row['title']) . '</a></td>';

				echo '<td class="windowbg2">' . nl2br(parse_bbc($row['description'])) . '</td>';
				
			}
			else
			{
				echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;cat=' . $row['ID_CAT'] . '">' . doUBBC($row['title']) . '</a></td>';

				echo '<td class="windowbg2">' . nl2br(doUBBC($row['description'])) . '</td>';

			}
			echo '<td class="windowbg2">' . $totallinks . '</td>';
			
			//Show Edit Delete and Order category
			echo '<td class="windowbg2"><a href="' . $scripturl . '?action=links;sa=catup;cat=' . $row['ID_CAT'] . ';a=admin">' . $txt['smflinks_txtup'] . '</a>&nbsp;<a href="' . $scripturl . '?action=links;sa=catdown;cat=' . $row['ID_CAT'] . ';a=admin">' . $txt['smflinks_txtdown'] .'</a></span>&nbsp;<a href="' . $scripturl . '?action=links;sa=editcat;cat=' . $row['ID_CAT'] . ';a=admin">' . $txt['smflinks_txtedit'] .'</a>&nbsp;<a href="' . $scripturl . '?action=links;sa=deletecat;cat=' . $row['ID_CAT'] . ';a=admin">' . $txt['smflinks_txtdel'] . '</a></td>';
			mysql_free_result($dbresult2);
			
			echo '</tr>';
		}
	
		mysql_free_result($dbresult);
		echo '<tr><td class="windowbg2" colspan="4" align="center">
			<a href="' . $scripturl . '?action=links;sa=addcat;a=admin">' . $txt['smflinks_addcat'] . '</a>
			
		</td></tr>
		</table>	
		</tr>
		<tr>
			<td class="windowbg">
			<b>' . $txt['smflinks_linkssettings'] . '</b> - <span class="smalltext">' . $txt['smflinks_linkssettings_des'] . '</span><br />
			<form method="post" action="' . $scripturl . '?action=links;sa=admin2">
			' . $txt['smflinks_setlinksperpage'] . '&nbsp;<input type="text" name="smflinks_setlinksperpage" value="' .  $modSettings['smflinks_setlinksperpage'] . '" /><br />
				
				<input type="checkbox" name="smflinks_setshowtoprate" ' . ($modSettings['smflinks_setshowtoprate'] ? ' checked="checked" ' : '') . ' />' . $txt['smflinks_setshowtoprate'] . '<br />
				<input type="checkbox" name="smflinks_setshowmostvisited" ' . ($modSettings['smflinks_setshowmostvisited'] ? ' checked="checked" ' : '') . ' />' . $txt['smflinks_setshowmostvisited'] . '<br />
				<input type="checkbox" name="smflinks_setshowstats" ' . ($modSettings['smflinks_setshowstats'] ? ' checked="checked" ' : '') . ' />' . $txt['smflinks_setshowstats'] . '<br />
				<input type="checkbox" name="smflinks_setallowbbc" ' . ($modSettings['smflinks_setallowbbc'] ? ' checked="checked" ' : '') . ' />' . $txt['smflinks_setallowbbc'] . '<br />
				
				<input type="submit" name="savesettings" value="' . $txt['smflinks_settings_save'] .'" />
			</form>
			</td>
		</tr>
			
			
		
		<tr>
		<td class="windowbg">
		<b>Has SMF Links helped you?</b> Then support the developers:<br />
    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
	<input type="hidden" name="cmd" value="_xclick">
	<input type="hidden" name="business" value="sales@visualbasiczone.com">
	<input type="hidden" name="item_name" value="SMF Links">
	<input type="hidden" name="no_shipping" value="1">
	<input type="hidden" name="no_note" value="1">
	<input type="hidden" name="currency_code" value="USD">
	<input type="hidden" name="tax" value="0">
	<input type="hidden" name="bn" value="PP-DonationsBF">
	<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" border="0" name="submit" alt="Make payments with PayPal - it is fast, free and secure!">
	<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
</form>
	</td>
	</tr>
		
	</table>';

	//The Copyright is required to remain or contact me to purchase link removal.
	echo '<div align="center"><a href="http://www.smfhacks.com" target="blank">SMF Links</a></div>';

}
?>